﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
using System.Data.SqlClient;
using Common.DataLayer;

namespace Common.Forms
{
    public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();
        }

        private void btnPrijaviSe_Click(object sender, EventArgs e)
        {
            #region Provera da li su popunjeni korisničko ime i lozinka
            if (String.IsNullOrEmpty(edtKorisnickoIme.Text))
            {
                this.erpLogin.SetError(edtKorisnickoIme, "Korisničko ime je obavezno!");
                this.DialogResult = DialogResult.None;
                return;
            }

            if (String.IsNullOrEmpty(edtLozinka.Text))
            {
                this.erpLogin.SetError(edtLozinka, "Lozinka je obavezna!");
                this.DialogResult = DialogResult.None;
                return;
            }
            #endregion

            using (CommonDataContext dc = new CommonDataContext())
            {
                #region Učitavanje korisnika
                var korisnik = dc.Korisniks.SingleOrDefault(c => c.KorisnickoIme == edtKorisnickoIme.Text);

                if (korisnik == null)
                {
                    MessageBox.Show("Ne postoji korisnik u bazi!");
                    this.DialogResult = DialogResult.None;
                    return;
                }
                
                string lozinka = korisnik.Lozinka;
                #endregion

                #region Hashovanje lozinke
                byte[] bytes = Encoding.Unicode.GetBytes(edtLozinka.Text);
                byte[] inArray = HashAlgorithm.Create("SHA1").ComputeHash(bytes);
                string lozinkaForma = Convert.ToBase64String(inArray);
                #endregion
               
                #region Pogrešna lozika
                if (!String.Equals(lozinkaForma, lozinka))
                {
                    this.erpLogin.SetError(edtLozinka, "Pogrešna lozinka!");
                    this.DialogResult = DialogResult.None;
                    return;
                }
                #endregion

                #region Pogrešan status
                if (!korisnik.Status)
                {
                    this.erpLogin.SetError(edtLozinka, "Korisnik nije aktivan!");
                    this.DialogResult = DialogResult.None;
                    return;
                }
                #endregion

                #region Učitavanje default vrednosti
                Common.Properties.Settings.Default.KorisnikId = korisnik.KorisnikId;
                Common.Properties.Settings.Default.JezikId = korisnik.JezikId;
                Common.Properties.Settings.Default.KorisnickoIme = korisnik.KorisnickoIme;
                Common.Properties.Settings.Default.KorisnikImePrezime = String.Format("{0} {1}", korisnik.Ime, korisnik.Prezime);
                #endregion
            }
        }

        #region Load
        private void Login_Load(object sender, EventArgs e)
        {
            #region Provera sql servera
            try
            {
                SqlConnection c = new SqlConnection(Common.Properties.Settings.Default.ConnectionString);
                c.Open();
            }
            catch (Exception ex)
            {
                Common.Tools.MessageError.Show(ex.Message);
                this.Close();
                return;
            }
            #endregion

            #if (DEBUG)
            edtKorisnickoIme.Text = "test";
            edtLozinka.Text = "test";
            #else
            edtKorisnickoIme.Text = "";
            edtLozinka.Text = "";
            #endif
        }
        #endregion
    }
}
